import goodsApi from '@/api/goods';
import modal from '@/plugins/modal.js'
import storage from '@/utils/storage'
import constant from '@/utils/constant'

const app = {
	state: {
		appLinkType: '1', // app下载页短链接类型
		topHeight: 0,
		statusBarHeight: 0,
		productTempCategoryList: storage.get(constant.productTempCategoryList), //商品分类模板列表
		productCategoryList: storage.get(constant.productCategoryList), //商品分类列表
		productCategoryFormatting: storage.get(constant.productCategoryFormatting), //商品分类列表格式化后的数据
		regionData: storage.get(constant.regionData), // 行政区划数据
		newReleaseProduct: "", //新品上架里面选中的商品信息
		refreshFlag: true, // 刷新，用于商品列表页面watch监听，变化时刷新页面
		refreshDetailFlag: true, // 刷新，用于商品详情页面watch监听，变化时刷新页面
		detailData: {}, //商品详情页数据
		detailOriginData: {}, //商品详情页历史数据
		detailAgree: false, //通过按钮是否启用
    carouselTypeData: { // 轮播图链接类型
      '1': '无',
      // '2': '活动',
      '3': '商品'
    },
		mallThemes: [
			{ value: 'violet', url: '/static/images/mall/violet.png', title: '方案一'},
			{ value: 'red', url: '/static/images/mall/red.png', title: '方案二'},
			{ value: 'green', url: '/static/images/mall/green.png', title: '方案三'},
			{ value: 'orange', url: '/static/images/mall/orange.png', title: '方案四'},
			{ value: 'blue', url: '/static/images/mall/blue.png', title: '方案五'}
		]
	},

	mutations: {
		SET_TOP_HEIGHT: (state, topHeight) => {
			state.topHeight = topHeight
		},
    SET_STATUSBAR_HEIGHT: (state, statusBarHeight) => {
			state.statusBarHeight = statusBarHeight
		},
		SET_CATEGORY_TEMP_LIST: (state, productTempCategoryList) => {
			state.productTempCategoryList = productTempCategoryList
      storage.set(constant.productTempCategoryList, productTempCategoryList)
		},
		SET_PRODUCT_CATEGORY_LIST: (state, productCategoryList) => {
			state.productCategoryList = productCategoryList
      storage.set(constant.productCategoryList, productCategoryList)
		},
		FORMATTING_PRODUCT_CATEGORY_LIST: (state, productCategoryList) => {
			const data = productCategoryList;
			const map = {};
			const result = [];
			data.forEach(item => {
				map[item.id] = item;
				item.children = [];
			});

			data.forEach(item => {
				if (item.parentId !== 0) {
					map[item.parentId] && map[item.parentId].children.push(item);
				} else {
					result.push(item);
				}
			});
			state.productCategoryFormatting = result
      storage.set(constant.productCategoryFormatting, result)
		},
		SET_REGION_DATA: (state, regionData) => {
			state.regionData = regionData
      storage.set(constant.regionData, regionData)
		},
		SET_NEW_RELEASE_PRODUCT: (state, newReleaseProduct) => {
			state.newReleaseProduct = newReleaseProduct
		},
		SET_REFRESH_FLAG: (state, refreshFlag) => {
			state.refreshFlag = refreshFlag
		},
		SET_REFRESH_DETAIL_FLAG: (state, refreshDetailFlag) => {
			state.refreshDetailFlag = refreshDetailFlag
		},
		SET_DETAIL_DATA: (state, detailData) => {
			state.detailData = detailData
		},
		SET_DETAIL_ORIGIN_DATA: (state, detailOriginData) => {
			state.detailOriginData = detailOriginData
		},
		SET_DETAIL_AGREE: (state, detailAgree) => {
			state.detailAgree = detailAgree
		},
	},

	actions: {
		getCategoryTempList({
			commit
		}) {
			try {
				goodsApi.getCategoryTempList().then((res) => {
					if (res.code === 0) {
						commit('SET_CATEGORY_TEMP_LIST', res.data)
					} else {
						modal.msgError(res.msg);
					}
				})
			} catch (e) {
				//TODO handle the exception
				console.log('getCategoryTempList ===>', e);
			}
		},
		getCategoryList({
			commit
		}) {
			try {
				goodsApi.getCategoryList().then((res) => {
					if (res.code === 0) {
						commit('SET_PRODUCT_CATEGORY_LIST', res.data)
						// 格式化分类列表
						commit('FORMATTING_PRODUCT_CATEGORY_LIST', res.data)
					} else {
						modal.msgError(res.msg);
					}
				})
			} catch (e) {
				//TODO handle the exception
				console.log('getCategoryList ===>', e);
			}
		},
		// 获取详情
		getGoodsDetail({
			commit,
			state
		}, productId) {
			modal.loading();
			try {
				goodsApi.getGoodsDetail(productId).then((res) => {
					modal.closeLoading();
					if (res.code === 0) {
						const productCategory = state.productCategoryList.filter((item) => item.id === res
							.data.categoryId);
						const obj = {
							...res.data,
							picUrl: JSON.parse(res.data.picUrl),
							productCategory: productCategory[0]?.name
						};
						commit('SET_DETAIL_DATA', obj)
						commit('SET_DETAIL_ORIGIN_DATA', JSON.parse(JSON.stringify(obj)))
					} else {
						modal.msgError(res.msg);
					}
				});
			} catch (e) {
				modal.closeLoading();
				console.log('getGoodsDetail===>', e);
			}
		}
	}
}

export default app